<?php if(!defined('CALL')) exit('No direct script access allowed');

require_once("cfg/cfg.php");
require_once("lib/mysql.class.php");

class Person
{
    
    public static function save($name_f, $name_l, $name_m, $phone = null, $email = null)
    {
        self::init();
        $sql = "INSERT INTO persons(name_f, name_l, name_m, phone, email) VALUES(%s, %s, %s, %s, %s)";
		$params = array(
			self::$db->escape($name_f),
			self::$db->escape($name_l),
			self::$db->escape($name_m),
			self::$db->escape($phone),
			self::$db->escape($email)
			);
		$sql = vsprintf($sql, $params);
        self::$db->query($sql);
    }
    
    public static function find($name_f, $name_l, $name_m, $phone = null, $email = null)
    {
        
    }
    
    public static function validate(&$params)
    {
        $buf = "";
        
        if(empty($params["name_f"])) $buf .= "\nНе указана фамилия";
        if(empty($params["name_l"])) $buf .= "\nНе указано имя";
        if(empty($params["name_m"])) $buf .= "\nНе указано отчество";
        
        $params["phone"] = format_phone($params["phone"]);
        if($params["phone"] === false) $buf .= "\nФормат номера телефона неверный";
        if(test_email($params["email"]) === false) $buf .= "\nФормат email неверный";
            
        if ($buf != "")
            return substr($buf, 1);
        else       
            return $buf;
    }
    
    private static function init()
    {
        if(is_null(self::$db))
        {
            MySQL::$url = DBURL;
            self::$db = MySQL::getInstance();
        }
    }
    
    private static $db = null;
}